package ph.com.gs3.formalistics.presenter;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.vo.business.User;
import ph.com.gs3.formalistics.presenter.fragements.workers.LoginWorkerFragment;
import ph.com.gs3.formalistics.presenter.fragements.workers.LoginWorkerFragment.LoginWorkerFragmentListener;
import ph.com.gs3.formalistics.presenter.fragments.views.LoginViewFragment;
import ph.com.gs3.formalistics.presenter.fragments.views.SplashViewFragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;

public class LoginActivity extends Activity implements LoginViewFragment.LoginActionsListener,
        LoginWorkerFragmentListener {

	public static final String TAG = LoginActivity.class.getSimpleName();

	// UI Fragments
	private LoginViewFragment loginFragment;
	private SplashViewFragment splashFragment;

	// Worker Fragments
	private LoginWorkerFragment loginWorkerFragment;

	public static final String FRAGMENT_TITLE_LOGIN = "Login to Formalistics";
	public static final String FRAGMENT_TITLE_SPLASH = "Formalistics";

	public static final String EXTRA_ERROR_MESSAGE = "error_message";

	// ========================================================================
	// View Methods

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// Initialize the fragments
		loginFragment = new LoginViewFragment();
		splashFragment = new SplashViewFragment();

		initializeWorkerFragment();

		// Initially show splash view
		showSplashView();

		// Have the application decide if it needs to show the login view or it can
		// directly go to the main view
		navigateOrShowLogin();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	// ========================================================================
	// {{ Functional Methods

	private void initializeWorkerFragment() {
		FragmentManager fragmentMan = getFragmentManager();

		loginWorkerFragment = (LoginWorkerFragment) fragmentMan
		        .findFragmentByTag(LoginWorkerFragment.TAG);

		if (loginWorkerFragment == null) {
			loginWorkerFragment = new LoginWorkerFragment();
			fragmentMan.beginTransaction().add(loginWorkerFragment, LoginWorkerFragment.TAG)
			        .commit();
			fragmentMan.executePendingTransactions();
		}
	}

	private void setFragment(Fragment fragment, String title) {

		FragmentTransaction transaction = getFragmentManager().beginTransaction();
		transaction.replace(R.id.container, fragment);

		transaction.commit();
		// transaction.commitAllowingStateLoss();
		setTitle(title);
	}

	private void navigateOrShowLogin() {

		User activeUser = loginWorkerFragment.getActiveUser();

		if (activeUser != null) {
			navigateToMainActivity(activeUser);
		} else {
			showLoginView();
		}

	}

	private void showSplashView() {
		setFragment(splashFragment, FRAGMENT_TITLE_SPLASH);
	}

	private void showLoginView() {
		setFragment(loginFragment, FRAGMENT_TITLE_LOGIN);
	}

	private void navigateToMainActivity(User activeUser) {

		// Start the main activity and close this activity
		Intent mainActivity = new Intent(LoginActivity.this, MainActivity.class);
		mainActivity.putExtra(MainActivity.EXTRA_LOGGED_IN_USER, activeUser);
		mainActivity.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(mainActivity);
		finish();

	}

	// }}

	// ========================================================================
	// UI Implementation Methods

	@Override
	public void onSignIn(String server, String email, String password) {

		if (splashFragment == null) {
			splashFragment = new SplashViewFragment();
		}

		showSplashView();
		splashFragment.setLoadingMessage("Logging in to server");

		loginWorkerFragment.login(server, email, password);

	}

	// ========================================================================
	// Worker Implementation Methods

	@Override
	public void onLoginSuccessful(User user) {
		navigateToMainActivity(user);
	}

	@Override
	public void onLoginFailed(String message) {
		showLoginView();
		loginFragment.setErrorMessage(message);
	}

}
